package com.xinruke.hostels.manage.user.controller;

import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.po.XtYewuyonghu;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.manage.user.interfaces.dto.*;
import com.xinruke.hostels.manage.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName UserInfoController
 * @CreateDate 2019/6/9
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 用户信息
 */
@RestController
@RequestMapping("user/info")
@Api(tags = "用户信息")
public class UserInfoController extends BaseController {

    @Resource
    private UserService userService;
    @Autowired
    OperateLogService operateLogService;

    @PostMapping(value = "/hotel/add")
    @ApiOperation(value = "添加酒店初始化用户信息")
    public ResultVO addHotelUserInfo(@Valid @RequestBody UserInfoAddDefaultDTO userInfoAddDTO) {
        ResultVO resultVO = userService.addDefaultUserInfo(userInfoAddDTO);
        return resultVO;
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "中央决策添加用户信息")
    public ResultVO addUserInfo(@Valid @RequestBody UserInfoAddDTO userInfoAddDTO) {
        userInfoAddDTO.setJiudianId(SysDicConstant.SUOSHUJIUDIAN);
        userInfoAddDTO.setSuoshuxitong(XtYewuyonghu.SUOSHUXITONG_ZHONGYANG);

        ResultVO resultVO = userService.addUserInfo(userInfoAddDTO);
        if (resultVO.getCode() == ResultVO.SUCCESS) {

            operateLogService.writeOperateLog("用户管理", "添加用户", "用户名[" + userInfoAddDTO.getYonghuming() + "]");
        }
        return resultVO;
    }

    @PostMapping(value = "/modify")
    @ApiOperation(value = "修改用户信息")
    public ResultVO modifyUserInfo(@Valid @RequestBody UserInfoModifyDTO userInfoModifyDTO) {
        ResultVO resultVO = userService.modifyUserInfo(userInfoModifyDTO);
        if (resultVO.getCode() == ResultVO.SUCCESS) {
            operateLogService.writeOperateLog("用户管理", "修改用户", "id[" + userInfoModifyDTO.getId() + "]");
        }
        return resultVO;
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除用户信息")
    public ResultVO deleteUserInfo(@Valid @RequestBody UserInfoDelDto infoDelDto) {
        ResultVO resultVO = ResultVO.fail();
        int i = userService.deleteUserInfo(infoDelDto);
        if (i > 0) {
            operateLogService.writeOperateLog("用户管理", "删除用户", "id[" + infoDelDto.getId() + "]");
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "查询用户列表")
    public ResultVO<RowsDataVO<UserInfoQueryResultDTO>> getUserInfoList(@RequestBody UserInfoQueryDTO userInfoQueryDTO) {
        ResultVO<RowsDataVO<UserInfoQueryResultDTO>> resultVO = ResultVO.fail();
        RowsDataVO<UserInfoQueryResultDTO> data = userService.getUserInfoList(userInfoQueryDTO);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(data);

        return resultVO;
    }

    @PostMapping(value = "/role")
    @ApiOperation(value = "查询指定用户的角色")
    public ResultVO<List<UserRolesQueryResultDto>> getUserRolesList(@RequestBody UserRolesQueryDto userInfoDetailDto) {
        ResultVO<List<UserRolesQueryResultDto>> resultVO = ResultVO.fail();
        List<UserRolesQueryResultDto> data = userService.getUserRolesList(userInfoDetailDto);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(data);

        return resultVO;
    }

    @PostMapping(value = "/detail")
    @ApiOperation(value = "查询用户详情")
    public ResultVO<UserDetailQueryResultDTO> detail(@Valid @RequestBody UserDetailQueryDTO userDetailQueryDTO) {
        ResultVO<UserDetailQueryResultDTO> resultVO = ResultVO.fail();
        UserDetailQueryResultDTO data = userService.detail(userDetailQueryDTO);
        if (data != null) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
            resultVO.setData(data);
        }
        return resultVO;
    }

    @PostMapping(value = "/updateUserPassword")
    @ApiOperation(value = "修改当前登录用户密码")
    public ResultVO updateUserPassword(@Valid @RequestBody UserPasswordUpdateDTO userPasswordUpdateDTO) {
        return userService.updateUserPassword(userPasswordUpdateDTO);
    }

}
